/**
 * Copyright (c) 2016-2019, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.web.theme;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

import cn.hg.base.common.BaseConstants;
import cn.hg.jfsa.core.option.Options;

public class ThemeManager {

	private static Log log = Log.getLog(ThemeManager.class);
	private static final ThemeManager me = new ThemeManager();

	public static ThemeManager me () {
		return ThemeManager.me;
	}

	private Theme currentTheme;

	private ThemeManager () {}

	public Theme getCurrentTheme () { return this.currentTheme; }

	public List<Theme> getInstalledThemes () {
		String basePath = PathKit.getWebRootPath() + BaseConstants.THEME_PATH;
		List<File> themeFolderList = new ArrayList<>();
		this.scanThemeFloders(new File(basePath), themeFolderList);
		List<Theme> themelist = null;

		if (themeFolderList.size() > 0) {
			themelist = new ArrayList<>();

			for (File themeFolder : themeFolderList) {
				themelist.add(new Theme(themeFolder + "/theme.config"));
			}

		}

		return themelist;
	}

	public Theme getThemeById (String id) {
		List<Theme> themes = this.getInstalledThemes();

		for (Theme theme : themes) {

			if (id.equals(theme.getId())) {
				return theme;
			}

		}

		return null;
	}

	public void init () {
		Theme theme = this.getThemeById(Options.get(BaseConstants.DEFAULT_THEME));
		// 使用默认主题ID初始化
		this.setCurrentTheme(theme);
	}

	public void setCurrentTheme (String themeId) throws ThemeException {

		if (StrKit.isBlank(themeId)) {
			throw new ThemeException("主题ID不能为空！");
		}

		// // 如果当前主题和目标主题相同
		// if (this.currentTheme != null && this.currentTheme.getId().equals(themeId)){
		// return;
		// }
		// 如果当前主题不存在则加载所有主题信息
		List<Theme> list = this.getInstalledThemes();
		// 遍历所有已安装主题
		Theme t = null;

		for (Theme theme : list) {

			if (themeId.equals(theme.getId())) {
				t = theme;
				break;
			}

		}

		// 没匹配到主题ID则抛出异常
		if (t == null) {
			ThemeManager.log.warn("主题 [" + themeId + "] 不存在！");
			throw new ThemeException("主题 [" + themeId + "] 不存在！");
		}

		this.currentTheme = t;
	}

	public void setCurrentTheme (Theme currentTheme) { this.currentTheme = currentTheme; }

	public void setOptions (Map<String, String> optionMap) {
		ThemeManager.me().getCurrentTheme().updateOptions(optionMap);
	}

	public void setOptions (Map<String, String> optionMap, String themeId) {
		Theme theme = this.getThemeById(themeId);

		if (theme == null) {
			return;
		}

		theme.updateOptions(optionMap);
	}

	private void scanThemeFloders (File file, List<File> list) {

		if (file.isDirectory()) {
			File configFile = new File(file, "theme.config");

			if (configFile.exists() && configFile.isFile()) {
				list.add(file);
			} else {
				File[] files = file.listFiles();

				if (null != files && files.length > 0) {

					for (File f : files) {

						if (f.isDirectory()) {
							this.scanThemeFloders(f, list);
						}

					}

				}

			}

		}

	}

}
